package com.onec.service.route.service.base;


import com.onec.service.api.dto.request.route.RouteReq;
import com.onec.service.api.dto.response.route.RouteResp;
import com.onec.service.api.enums.ServiceTradeEnum;
import com.onec.service.api.enums.SystemTypeEnum;
import com.onec.service.api.enums.error.RouteErrorEnum;
import com.onec.service.route.entity.RouteChannelInfo;
import com.onec.service.route.entity.TradeConfigInfo;
import com.onec.service.route.entity.TradeRuleInfo;
import com.onec.service.route.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;

import java.util.List;


/**
 * 机构接口返回基类：
 *
 * @author onec
 * @since 20210107
 */
@Slf4j
public class BaseRouteRiskService {

    @Autowired
    protected ApplicationContext applicationContext;
    @Autowired
    protected TradeConfigInfoService tradeConfigInfoService;
    @Autowired
    protected TradeRuleService tradeRuleService;
    @Autowired
    protected RouteChannelInfoService routeChannelInfoService;
    @Autowired
    protected RouteChannelBankService routeChannelBankService;
    @Autowired
    protected BusinessInfoService businessInfoService;

    /**
     * @param routeErrorEnum
     * @return RouteResp
     */
    protected RouteResp respErrorRoute(RouteErrorEnum routeErrorEnum) {
        RouteResp routeResp = new RouteResp();
        routeResp.setCode(routeErrorEnum.getCode());
        routeResp.setMessage(routeErrorEnum.getMessage());

        return routeResp;
    }

    /**
     * @param routeChannelInfo
     * @return RouteResp
     */
    protected RouteResp respRouteChannelInfo(RouteReq routeReq, RouteChannelInfo routeChannelInfo) {
        RouteResp routeResp = respErrorRoute(RouteErrorEnum.SUCCESS);
        routeResp.setBusinessNo(routeChannelInfo.getBusinessNo());

        routeResp.setChannelBusinessNo(routeChannelInfo.getChannelBusinessNo());
        routeResp.setFee(routeChannelInfo.getFee());
        routeResp.setRate(routeChannelInfo.getRate());

        routeResp.setBankCode(routeReq.getBankCode());
        routeResp.setMerchantNo(routeReq.getMerchantNo());

        return routeResp;
    }

    /**
     * 验证交易：商户：配置风控规则：
     *
     * @param routeReq       风控信息：
     * @param systemTypeEnum 商户配置：交易配置：
     */
    protected void checkRouteRiskType(RouteReq routeReq, SystemTypeEnum systemTypeEnum) {
        // 业务配置的规则：
        TradeConfigInfo tradeConfigInfo = tradeConfigInfoService
                .findByBusinessNoType(routeReq.getBusinessNo(), systemTypeEnum);
        log.info("类型：{},当前业务:{},业务可用条数：{}", systemTypeEnum, routeReq.getBusinessNo(), 1);

        List<TradeRuleInfo> tradeRuleInfos = tradeRuleService.getTradeRuleList(tradeConfigInfo.getId());
        if (tradeRuleInfos.size() > 0) {
            log.info("执行业务可用条数限制：第：{}：条：配置的规则条数：{}", "", tradeRuleInfos.size());
            int ruleSize = 1;
            for (TradeRuleInfo tradeRuleInfo : tradeRuleInfos) {
                log.info("执行规则限制：第：{}：条：规则：{}", ruleSize, tradeRuleInfo.getValue());
                ServiceTradeEnum serviceTradeEnum = tradeRuleInfo.getServiceTrade();
                TradeConfigBaseService tradeConfigBaseService = applicationContext.getBean(serviceTradeEnum.name(),
                        TradeConfigBaseService.class);
                tradeConfigBaseService.beforeExecute(routeReq, tradeRuleInfo);
                ruleSize++;
            }
        }
    }
}
